<template>
  <div class="page-container">
    <!-- 签到按钮区域 -->
    <div class="action-card">
      <div
          v-if="!isDrawing"
          class="sign-in-btn neon-effect"
          >
        <span class="btn-text">请及时签到</span>
        <div class="pulse-effect"></div>
      </div>
    </div>

    <!-- 画板模态框 -->
    <div v-if="isDrawing" class="drawing-modal">
      <div class="drawing-content">
        <h3 class="drawing-title">请绘制签到数字</h3>
        <canvas
            ref="canvas"
            class="drawing-canvas"
            width="400"
            height="400"></canvas>
        <el-button
            class="finish-btn"
            type="success"
            @click="finishDrawing">
          <i class="el-icon-check"></i>
          点击签到
        </el-button>
      </div>
    </div>

    <!-- 数据表格区域 -->
    <el-card class="data-card">
      <div slot="header" class="table-header">
        <span class="header-title">签到记录</span>
        <div class="header-extra">
          <el-button
              type="text"
              icon="el-icon-refresh"
              @click.native="getPageList"></el-button>
        </div>
      </div>
      <el-table
          :data="listData"
          style="width: 100%"
          class="custom-table"
          stripe
          v-loading="loading">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="signTime" label="签到时间" width="180"></el-table-column>
        <el-table-column prop="remake" label="说明" min-width="180"></el-table-column>
        <el-table-column prop="clazzId" label="班级码" width="120"></el-table-column>
        <el-table-column prop="number" label="签到数字" width="100"></el-table-column>
        <el-table-column prop="signNumber" label="签到码" width="100"></el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click.native="startDrawing(scope.row)">
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
            background
            :page-sizes="[10, 20, 30, 50]"
            :page-size="pageInfo.pageSize"
            :current-page.sync="pageInfo.pageNum"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="sizeChange"
            @current-change="currentChange">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import utils from "@/utils/utils";

export default {
  data() {
    return {
      isDrawing: false,
      base64Data: '',
      ctx: null,
      lastX: 0,
      lastY: 0,
      isDrawingNow: false,
      number: "",
      signNumber:"",
      clazzId: utils.getUserInfo().clazzId,
      username:utils.getUserInfo().username,
      total: 0,
      listData: [],
      loading: false,
      pageInfo: {
        pageNum: 1,
        pageSize: 10
      }
    };
  },
  created() {
    this.getPageList();
  },
  methods: {
    startDrawing(row) {
      this.isDrawing = true;
      this.number = row.number;
      this.signNumber = row.signNumber;
      this.$nextTick(() => {
        this.initCanvas();
      });
    },
    initCanvas() {
      this.ctx = this.$refs.canvas.getContext('2d');
      this.ctx.lineWidth = 15;
      this.ctx.lineCap = 'round';
      this.ctx.strokeStyle = 'white';
      this.ctx.fillStyle = 'black';
      this.ctx.fillRect(0, 0, this.$refs.canvas.width, this.$refs.canvas.height);

      const canvas = this.$refs.canvas;
      canvas.addEventListener('mousedown', this.handleMouseDown);
      canvas.addEventListener('mousemove', this.handleMouseMove);
      canvas.addEventListener('mouseup', this.handleMouseUp);
      canvas.addEventListener('mouseout', this.handleMouseUp);
    },
    handleMouseDown(event) {
      this.isDrawingNow = true;
      this.lastX = event.offsetX;
      this.lastY = event.offsetY;
    },
    handleMouseMove(event) {
      if (this.isDrawingNow) {
        const x = event.offsetX;
        const y = event.offsetY;

        this.ctx.beginPath();
        this.ctx.moveTo(this.lastX, this.lastY);
        this.ctx.lineTo(x, y);
        this.ctx.stroke();

        this.lastX = x;
        this.lastY = y;
      }
    },
    handleMouseUp() {
      this.isDrawingNow = false;
    },
    finishDrawing() {
      this.base64Data = this.$refs.canvas.toDataURL();
      this.isDrawing = false;
      this.submitFaceData();
    },
    getPageList() {
      this.loading = true;
      this.$axios.get("/sign/getByClazzId", {
        params: {
          pageInfo: this.pageInfo,
          clazzId: this.clazzId
        }
      }).then(res => {
        this.listData = res.data.list;
        this.total = res.data.total;
      }).finally(() => {
        this.loading = false;
      });
    },
    currentChange(e) {
      this.pageInfo.pageNum = e;
      this.getPageList();
    },
    sizeChange(e) {
      this.pageInfo.pageSize = e;
      this.getPageList();
    },
    submitFaceData() {
      this.$axios.post("/sign/doSign", {
        base64Data: this.base64Data,
        number: this.number,
        username:this.username,
        clazzId:this.clazzId,
        signNumber: this.signNumber
      }).then(res => {
        if (res.code === 200) {
          this.$message.success(res.msg);
          this.getPageList();
        }
      });
    }
  },
  beforeDestroy() {
    const canvas = this.$refs.canvas;
    if (canvas) {
      canvas.removeEventListener('mousedown', this.handleMouseDown);
      canvas.removeEventListener('mousemove', this.handleMouseMove);
      canvas.removeEventListener('mouseup', this.handleMouseUp);
      canvas.removeEventListener('mouseout', this.handleMouseUp);
    }
  }
};
</script>

<style scoped>
/* 保持原有样式不变 */
.page-container {
  padding: 24px;
  background: #f5f7fa;
  min-height: 100vh;
}

.action-card {
  margin-bottom: 24px;
  text-align: center;
  padding: 20px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}

.sign-in-btn {
  display: inline-flex;
  width: 160px;
  height: 160px;
  border-radius: 50%;
  background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
  color: white;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  transition: transform 0.3s ease;
}

.btn-text {
  font-size: 24px;
  font-weight: 500;
  letter-spacing: 2px;
  z-index: 1;
}

.neon-effect {
  box-shadow: 0 0 20px rgba(76,175,80,0.3);
}

.pulse-effect {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: rgba(76,175,80,0.2);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% { transform: scale(0.8); opacity: 1; }
  100% { transform: scale(1.5); opacity: 0; }
}

.drawing-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

.drawing-content {
  background: white;
  padding: 24px;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
  text-align: center;
}

.drawing-title {
  color: #303133;
  margin-bottom: 20px;
}

.drawing-canvas {
  border: 2px solid #ebeef5;
  border-radius: 8px;
  background: #fff;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.finish-btn {
  margin-right: 3%;
  font-size: 16px;
  border-radius: 25px;
  transition: all 0.3s;
}

.data-card {
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 0;
}

.header-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.custom-table {
  border-radius: 8px;
  overflow: hidden;
}

.custom-table::before {
  display: none;
}

.pagination-container {
  margin-top: 24px;
  display: flex;
  justify-content: flex-end;
}

.el-button--mini {
  padding: 6px 12px;
  border-radius: 8px;
  transition: all 0.3s;
}

.el-button--primary {
  background: linear-gradient(135deg, #409eff, #337ecc);
  border: none;
}

@media (max-width: 768px) {
  .page-container {
    padding: 12px;
  }

  .sign-in-btn {
    width: 120px;
    height: 120px;
  }

  .btn-text {
    font-size: 18px;
  }
}
</style>